package cc.rengu.igas.channel.cpcn.core.service.mchntorder;

import cc.rengu.igas.channel.cpcn.common.constant.CpcnParamConstant;
import cc.rengu.igas.channel.cpcn.common.constant.CpcnTreeNodeConstant;
import cc.rengu.igas.channel.cpcn.core.service.base.CpcnBaseService;
import cc.rengu.igas.channel.cpcn.facade.request.CpcnMchntOrderPayerAcctInfoQueryRequest;
import cc.rengu.igas.channel.cpcn.facade.response.CpcnMchntOrderPayerAcctInfoQueryResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/22 9:20
 */
public class CpcnMchntOrderPayerAcctInfoQueryService extends CpcnBaseService {


    @Override
    protected CpcnMchntOrderPayerAcctInfoQueryRequest beforeBizProcess() throws Exception {
        CpcnMchntOrderPayerAcctInfoQueryRequest request = new CpcnMchntOrderPayerAcctInfoQueryRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTree = new XmlTreeUtil();
        CpcnMchntOrderPayerAcctInfoQueryRequest cpcnMchntOrderPayerAcctInfoQueryRequest = (CpcnMchntOrderPayerAcctInfoQueryRequest) request;
        String institutionId = cpcnMchntOrderPayerAcctInfoQueryRequest.getInstitutionId();
        String paymentNo = cpcnMchntOrderPayerAcctInfoQueryRequest.getPaymentNo();
        BizResponse<CpcnMchntOrderPayerAcctInfoQueryResponse> bizResponse = new BizResponse<>();
        CpcnMchntOrderPayerAcctInfoQueryResponse response = new CpcnMchntOrderPayerAcctInfoQueryResponse();
        if (StringUtil.isNullorEmpty(institutionId) ||
                StringUtil.isNullorEmpty(paymentNo)) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("缺少一个或多个业务参数！");
            response.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
            response.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        rglog.trace("检查请求通过");

        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.TX_CODE, cpcnMchntOrderPayerAcctInfoQueryRequest.getHeader().getTxnNum());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID, cpcnMchntOrderPayerAcctInfoQueryRequest.getInstitutionId());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.PAYMENT_NO, cpcnMchntOrderPayerAcctInfoQueryRequest.getPaymentNo());

        /* TODO 演示环境默认返回成功 START */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            rglog.info("演示环境默认返回成功");
            response.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            response.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        /* TODO 演示环境默认返回成功 END */

        try {
            int retCode = schedule.callService(CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, CpcnParamConstant.CPCN_COMMUNICATION_SVC_LABEL);
            /*判断retCode返回值*/
            /*通讯成功*/
            if (ScheduleImpl.SERVICE_SUCCESS == retCode) {
                /*判断状态码*/
                String respCode = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_CODE);
                if (CpcnParamConstant.CPCN_RESP_SUCC.equals(respCode)) {
                    rglog.debug("调用通道业务成功");
                    //通道调用成功的应答处理
                    xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.TX_CODE, cpcnMchntOrderPayerAcctInfoQueryRequest.getHeader().getTxnNum());
                    xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID, cpcnMchntOrderPayerAcctInfoQueryRequest.getInstitutionId());
                    xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.PAYMENT_NO, cpcnMchntOrderPayerAcctInfoQueryRequest.getPaymentNo());
                    ConvertUtil.convertOutput(response);
                    bizResponse.setResult(response);
                } else {
                    rglog.debug("调用通道业务失败");
                    String code = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_CODE);
                    if (StringUtil.isNullorEmpty(code)) {
                        response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespCode(code);
                    }
                    String desc = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_DESC);
                    if (StringUtil.isNullorEmpty(desc)) {
                        response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespDesc(desc);
                    }
                }
            } else if (ScheduleImpl.SERVICE_TIMEOUT == retCode) {
                //调用超时 赋值应答
                rglog.error("callService {} 失败！返回值={}", CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            } else {
                //调用明确失败
                rglog.error("callService {} 失败！返回值={}", CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            }
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        bizResponse.setResult(response);
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        CpcnMchntOrderPayerAcctInfoQueryResponse cpcnMchntOrderPayerAcctInfoQueryResponse = (CpcnMchntOrderPayerAcctInfoQueryResponse) response.getResult();
        ConvertUtil.convertInput(cpcnMchntOrderPayerAcctInfoQueryResponse);
    }


}
